#!/usr/bin/env bash
set -e
set -u
set -o pipefail


source "${BASH_SOURCE%/*}/assertions.sh"


test_get_absolute_path() {
    local current_dir=${1:?}
    local relative_dir=${2:?}
    source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
    get_absolute_path "$current_dir" "$relative_dir"
}


test_get_absolute_path_old() {
    assert_equal \
        "test_get_absolute_path" \
        "/glux" \
        "$(test_get_absolute_path "/" "glux")"
    assert_equal \
        "test_get_absolute_path" \
        "/foo/bar/." \
        "$(test_get_absolute_path "/foo/bar" ".")"
    assert_equal \
        "test_get_absolute_path" \
        "/foo/bar/glux" \
        "$(test_get_absolute_path "/foo/bar" "glux")"
}


test_get_absolute_path_new() {
    source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
    local beg_dir=${1:?}


    test "$(get_absolute_path_better "/")" == "/" || \
        echo "root path failed"
    test "$(get_absolute_path_better ".")" == "$(pwd)" || \
        echo "current directory failed"
    test "$(get_absolute_path_better "..")" == "$(dirname "$(pwd)")" || \
        echo "parent directory failed"
    test "$(get_absolute_path_better "./test_get_absolute_path")" == "$(pwd)/test_get_absolute_path" || \
        echo "path to this file failed"
    test "$(get_absolute_path_better "../README.org")" == "$(dirname "$(pwd)")/README.org" || \
        echo "path to sibling file failed"


    echo "INFO: Expect output to stderr:"
    test "$(get_absolute_path_better)" == "" || echo "no input failed"
    test "$(get_absolute_path_better "")" == "" || echo "empty string failed"
    test "$(get_absolute_path_better "/probably_no_such_path")" == "" || echo "bad path failed"


    if [[ "$beg_dir" != "$(pwd)" ]]; then
        echo "WARNING: We changed directories"
    fi
}


test_get_absolute_path_main() {
    echo "${FUNCNAME[0]}"
    test_get_absolute_path_old
    test_get_absolute_path_new "$(pwd)"
}


test_get_absolute_path_main
